package com.project.service.impl;

import com.project.bean.CutPageBean;
import com.project.bean.StudentBean;
import com.project.mapper.IStudentMapper;
import com.project.service.IStudentService;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

public class StudentServiceImpl extends  BaseService implements IStudentService {
    @Override
    public void add(StudentBean student) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.add(student);
        session.commit();
        session.close();
    }

    @Override
    public void update(int id, String tel) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.update(id, tel);
        session.commit();
        session.close();
    }

    @Override
    public List<StudentBean> findByName(String name) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
//        List<StudentBean> list = mapper.findByName("%"+name+"%");
        List<StudentBean> list = mapper.findByName(name);
        session.close();
        return list;
    }

    @Override
    public StudentBean findById(int id) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        StudentBean student =  mapper.findById(id);
        session.close();
        return student;
    }

    @Override
    public List<StudentBean> findByBirthday(LocalDate startDate, LocalDate endDate) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        List<StudentBean> list = mapper.findByBirthday(startDate, endDate);
        session.close();
        return list;
    }

    @Override
    public void addMore(List<StudentBean> list) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.addMore(list);
        session.commit();
        session.close();
    }

    @Override
    public List<StudentBean> findByItem(String name, LocalDate startDate, LocalDate endDate) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        List<StudentBean> list = mapper.findByItem(name, startDate, endDate);
        session.close();
        return list;
    }

    @Override
    public CutPageBean<StudentBean> findAll(int pageNO) {
        CutPageBean<StudentBean> cutBean = new CutPageBean<StudentBean>();
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        cutBean.setList(mapper.cutAllList((pageNO - 1)*CutPageBean.PAGESIZE, CutPageBean.PAGESIZE));
        cutBean.setCount(mapper.cutAllCount());

        session.close();
        return cutBean;
    }

    @Override
    public CutPageBean<StudentBean> cutByItem(int pageNO, String name, LocalDate startDate, LocalDate endDate) {
        CutPageBean<StudentBean> cutBean = new CutPageBean<StudentBean>();
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        cutBean.setList(mapper.cutByItem((pageNO - 1)*CutPageBean.PAGESIZE, CutPageBean.PAGESIZE,name,startDate,endDate));
        cutBean.setCount(mapper.cutByCount(name, startDate, endDate));

        session.close();
        return cutBean;
    }

    public static void main(String[] args) {
        IStudentService service = new StudentServiceImpl();
//        service.add(new StudentBean("胡青牛", "13029382832", LocalDate.parse("1985-09-30")));
//        service.update(1, "1892382343");
//        System.out.println(service.findByName("张'"));
//        System.out.println(service.findById(1));
//        System.out.println(service.findByBirthday(LocalDate.parse("1990-01-01"), LocalDate.parse("1995-01-01")));
//        service.addMore(Arrays.asList(new StudentBean("aaa","1234234324",LocalDate.parse("1998-02-03")),
//                new StudentBean("bbb","45456456",LocalDate.parse("1999-04-05")),
//                new StudentBean("ccc","87789879",LocalDate.parse("1996-09-08"))));

        System.out.println( service.cutByItem(1,"", LocalDate.parse("1990-10-01"), LocalDate.parse("1994-01-01")));
//        System.out.println(service.findAll(1));
    }
}
